package com.zhangye.test.api;

import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zhangye.test.model.Admin;
import com.zhangye.test.service.IosPaySucService;
import com.zhangye.test.service.SafeSessionService;


@Controller
@RequestMapping("api/paySuc")
public class IosPaySuc {

	@Autowired
	private SafeSessionService safeSessionService;// 安全登录
	
	@Autowired
	private IosPaySucService iosPaySucService;
	
	
	/**
	 * 成功回调
	 * 作者： 张叶
	 * 创建时间：2018年6月6日  下午3:55:18
	 */
	@ResponseBody
	@RequestMapping(value = "/successNew", method = RequestMethod.POST)
	public ApiResult successNew(HttpServletRequest request,String datas) {
		
		ApiResult result = new ApiResult();
		String reqParam = request.getParameter("datas");
		Admin m = safeSessionService.getCurrent(request);
		if (m != null) {
			JSONObject  jasonObject = JSONObject.fromObject(reqParam);
			Map<String, Object>  map = new HashMap<String,Object>();
			try {
				map = iosPaySucService.successNew(jasonObject, m.getId());
			} catch (ParseException e) {
				e.printStackTrace();
			}
			if (MapUtils.isNotEmpty(map)) {
				if (map.get("status").equals("0")) {
					result.msg.desc = "成功";
					result.msg.status = 0;
				} else if(map.get("status").equals("1")){
					result.msg.desc = "月数错误，请重试";
					result.msg.status = 1;
				} else {
					result.msg.desc = "系统错误，请重试";
					result.msg.status = 1;
				}
			} else {
				result.msg.desc = "系统错误，请重试";
				result.msg.status = 1;
			}
		} else {
			result.msg.desc = "当前登录已失效，请重新登录 ";
			result.msg.status = 2;
		}
		return result;
	}
}
